package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Suggest;
import com.itheima.service.SuggestService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/suggest")
public class SuggestController {
    @Reference
    private SuggestService suggestService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//页码
        Integer pageSize = queryPageBean.getPageSize();//总记录数
        String queryString = queryPageBean.getQueryString();//条件
        return suggestService.findPage(currentPage, pageSize, queryString);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Suggest suggest) {
        try {
            suggestService.add(suggest);
        } catch (Exception e) {
            return new Result(false, "新增评估建议失败");
        }
        return new Result(true, "新增评估建议成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Suggest byId = suggestService.findById(id);
            return new Result(true,"查询评估建议成功",byId);
        } catch (Exception e) {
            return new Result(false,"查询评估建议失败");
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Suggest suggest) {
        try {
            suggestService.edit(suggest);
            return new Result(true, "编辑评估建议成功");
        } catch (Exception e) {
            return new Result(false, "编辑评估建议成功");
        }
    }


    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            suggestService.delete(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            return new Result(false, "删除评估建议失败");
        }
        return new Result(true, "删除评估建议成功");

    }

}
